import { getpermissionsAPI } from "@/api/user";
import { routes, resetRouter } from "@/router/index";

export default {
  namespaced: true,
  state: () => {
    return {
      // 权限标识
      permissions: [],
      // 路由规则
      menuList: [],
    };
  },
  getters: {},
  mutations: {
    // 修改权限标识
    setpermissions(state, permissions) {
      state.permissions = permissions;
    },
    // 修改路由规则
    setMenuList(state, asyncRoutes) {
      state.menuList = [...routes, ...asyncRoutes];
    },
    // 清空路由规则
    clearMenuList(state) {
      state.menuList = [];
      resetRouter()
    },
  },
  actions: {
    async getpermissions(store) {
      const permissions = (await getpermissionsAPI()).data.permissions;
      store.commit("setpermissions", permissions);
      // 可以直接返回给调用actions函数的地方
      return permissions;
    },
  },
};
